//: [Previous](@previous)

import Foundation

var str = "Hello, playground"

//: [Next](@next)


//将Date对象转换为字符串
let dateFormatter = DateFormatter()
//设置字符串的日期格式
dateFormatter.dateFormat = "yyyy-MM-dd 'at' HH:mm"
//获取格式化的字符串
let date = Date()
let dateString = dateFormatter.string(from: date)


//将日期转化字符串的实用扩展
extension Date{
    func toString() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        return dateFormatter.string(from: self)
    }
}
let date1 = Date()
let dateString1 = date1.toString()

let date3 = Date()
let date4 = Date()

if date3 < date4 {
    print("true")
}else{
    print("false")
}

//比较日期并处理秒、周、月和年
let dateStringUTC = "2016-10-22 12:37:48 +0000"
let dateFormatterUTC = DateFormatter()
dateFormatterUTC.locale = Locale(identifier: "en_US_POSIX")
dateFormatterUTC.dateFormat = "yyyy-MM-dd HH:mm:ss X"
let dateUTC = dateFormatterUTC.date(from: dateStringUTC)!

let now = Date()

let nowFormatter = DateComponentsFormatter()
nowFormatter.unitsStyle = .full
nowFormatter.maximumUnitCount = 2
let string = nowFormatter.string(from: dateUTC, to: Date())! + " " + NSLocalizedString("ago", comment: "added after elapsed time to say how long before")

//获了当前时间的时间戳
let timeString = "1268123281843"
let interval = Date().timeIntervalSince(date1)
let timeStamp = Date().timeIntervalSince1970

//将时间戳转换为时间
let interval2 = TimeInterval(timeString)! / 1000
let stringTime =  dateFormatterUTC.string(from: Date(timeIntervalSince1970: interval2))






















